package com.nolon.flange.adapter

import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.nolon.flange.R
import com.nolon.flange.databinding.ItemPreviewBinding
import com.nolon.flange.model.RawSolution
import com.google.android.material.textview.MaterialTextView

class PreviewAdapter(private val acc: AppCompatActivity) :
    RecyclerView.Adapter<PreviewAdapter.PreviewViewHolder>() {

    private var tableHeaderColor: Int = ContextCompat.getColor(acc, R.color.table_1_header)
    private var tableRowOddColor: Int = ContextCompat.getColor(acc, R.color.table_1_row_1)
    private var tableRowEvenColor: Int = ContextCompat.getColor(acc, R.color.table_1_row_2)
    private var contentColor: Int = ContextCompat.getColor(acc, R.color.black)

    private val tailColor: Int = ContextCompat.getColor(acc, R.color.grey_500)

    private var componentColor = tableRowEvenColor

    private var solutionList: ArrayList<RawSolution> = arrayListOf()
    private var unusedList: ArrayList<Int> = arrayListOf()

    fun swapData(list: List<RawSolution>) {
        solutionList.clear()
        unusedList.clear()
        solutionList.addAll(list)
        list.forEach {
            unusedList.add(it.rawInfo.length - it.rawInfo.usedLength())
        }
        notifyDataSetChanged()
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PreviewViewHolder {
        val binding =
            ItemPreviewBinding.inflate(LayoutInflater.from(parent.context), parent, false)
//                LayoutInflater.from(parent.context).inflate(R.layout.item_preview, parent, false)
        return PreviewViewHolder(binding)

    }

    override fun onBindViewHolder(holder: PreviewViewHolder, position: Int) {
        val solution = solutionList[position]
        val tailLen = unusedList[position]
        val raw = solution.rawInfo
        val parts = solution.rawInfo.parts

        with(holder) {
            binding.textPreview.text = raw.info()
            // 防止多次添加
            binding.linearContainer.removeAllViews()
            parts.forEach { part ->
                val view = createTextView(part.quantity, raw.length)
                binding.linearContainer.addView(view)
            }
            val tail = createTextView(tailLen, raw.length, true)
            binding.linearContainer.addView(tail)
        }
    }

    override fun getItemCount() = solutionList.size

    override fun getItemViewType(position: Int) = if (position == 0) HEADER_TYPE else CONTENT_TYPE

    class PreviewHeaderViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val layout: LinearLayoutCompat = view.findViewById(R.id.layout)
    }

    class PreviewViewHolder(val binding: ItemPreviewBinding) : RecyclerView.ViewHolder(binding.root)

    companion object {
        const val HEADER_TYPE = 0
        const val CONTENT_TYPE = 1
    }

    private fun createTextView(
        partLen: Int,
        totalLen: Int,
        isTail: Boolean = false
    ): MaterialTextView {
        val component = MaterialTextView(acc).apply {
            if (partLen / totalLen > 0.05) {
                text = partLen.toInt().toString()
                setTextColor(contentColor)
            }
            ellipsize = TextUtils.TruncateAt.END
            gravity = Gravity.CENTER
            maxLines = 1
            setPadding(0, 20, 0, 20)
            componentColor = when {
                isTail -> {
                    setBackgroundColor(tailColor)
                    tableRowEvenColor
                }
                else -> {
                    if (componentColor == tableRowOddColor) {
                        setBackgroundColor(tableRowEvenColor)
                        tableRowEvenColor
                    } else {
                        setBackgroundColor(tableRowOddColor)
                        tableRowOddColor
                    }
                }
            }
            layoutParams =
                LinearLayoutCompat.LayoutParams(
                    0,
                    100,
//                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    partLen.toFloat() / totalLen.toFloat()
                )
        }
        return component
    }

}